const jwt = require('jsonwebtoken')
const jwtKey = 'xiaochuExchange'
module.exports = class {
    constructor() {
    }
    jwtSign(data) {
        const token = jwt.sign(data, jwtKey, { expiresIn: '3h' })
        return token
    }
    // token验证函数(jwtCheck)
    jwtCheck(req, res, next) {
        //前端headers传来的token值:
        if (req.headers.authorization == undefined) {
            return res.send({
                code: 402,
                msg: 'token无效'
            })
        }
        const token = req.headers.authorization.split(" ")[1];
        jwt.verify(token, jwtKey, (err, data) => {
            if (err) {
                res.send({
                    code: 401,
                    msg: 'token已过期'
                })
            } else {
                req.use = data
                next()
            }
        })
    }
}